package com.fingard.dsp.bank.directbank.hfrcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by F on 2020/2/4.
 */
public class HFRCB01SinglePay extends HFRCB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            HFRCB01Header header = new HFRCB01Header();
            header.reqTime =p_req1908.singleDetail.getPayDateTime();
            header.serialNo = p_req1908.singleDetail.askNumber;
            if("1".equals(p_req1908.singleDetail.sameBnk)) {
                header.trsCode = "B2EBankInnerTransfer";
            }else{
                header.trsCode = "B2ECrossBankTransfer";
            }
            writeCommonHeader(xmlWriter,header);
            XElement main_data = new XElement("Body","");
//            SysFlag	系统标志	String(1)	M
            if("1".equals(p_req1908.singleDetail.sameBnk)) {
                main_data.addChild(new XElement("SysFlag","0"));
            }else{
                main_data.addChild(new XElement("SysFlag","1"));
//                Priority	转账方式	String(1)	O
                if(("1".equals(p_req1908.singleDetail.paySpd))||
                        (new BigDecimal(p_req1908.singleDetail.payAmount).compareTo(new BigDecimal("50000.00"))!=-1)){
                    main_data.addChild(new XElement("Priority","1"));
                }else{
                    main_data.addChild(new XElement("Priority","0"));
                }
//                UnionDeptId	收款人行号	String(12)	M
                main_data.addChild(new XElement("UnionDeptId",p_req1908.singleDetail.cnapsCode));
            }
//            PayerAcNo	付款人账号	String(32)	M
            main_data.addChild(new XElement("PayerAcNo",p_req1908.singleDetail.payAct));
//            PayerAcName	付款人名称	String(100)	M
            main_data.addChild(new XElement("PayerAcName",p_req1908.singleDetail.payName));
//            PayerCurrency	交易币种	Strig(3)	M
            main_data.addChild(new XElement("PayerCurrency",convertCurToBank(p_req1908.singleDetail.payCur)));
//            PayeeAcNo	收款人账号	String(32)	M
            main_data.addChild(new XElement("PayeeAcNo",p_req1908.singleDetail.recAct));
//            PayeeAcName	收款人户名	String(100)	M
            main_data.addChild(new XElement("PayeeAcName",p_req1908.singleDetail.recName));
//            Amount	转账金额	Double(15,2)	M
            main_data.addChild(new XElement("Amount",p_req1908.singleDetail.payAmount));
//            Remark	付款用途	String(60)	M
            main_data.addChild(new XElement("Remark",p_req1908.singleDetail.purpose));
            nodeAnalysis(p_req1908.singleDetail.srcXElement,main_data,getCharset());
            xmlWriter.writeXElement(main_data);
            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString(),true);
//            String[] bankResult = new String[]{"", ""};
//            bankResult[1] = FileHelper.readFileText("C:\\Users\\F\\Desktop\\tmp.xml", "gbk");
//            WriteBankLogStep3(bankResult[1]);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(tmpBkReader!=null){
                    String rsp_no = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                    String rsp_msg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                    if ("000000".equals(rsp_no)) {
                        rsp_no = rsp_no+"+"+tmpBkReader.getFirstText("/Message/Body/Status");
                    }
                    detail.respCode = "0";
                    detail.respInfo = rsp_msg;
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;
                    detail.payInfoCode = rsp_no;
                    detail.transState = rsp_no;
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            } else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try{
            XmlTextWriter xmlWriter = new XmlTextWriter();
            HFRCB01Header header = new HFRCB01Header();
            header.reqTime =new Date();
            header.serialNo = getBatchID(header.reqTime);
            header.trsCode = "B2ETransResultQry";
            writeCommonHeader(xmlWriter,header);
            xmlWriter.writeStartElement("Body");
//            OrgSerialNo	原交易流水号	String(20)
            xmlWriter.writeElementString("OrgSerialNo", p_req1944.askNumber);
//            OrgReqTime	原交易时间	String(14)	M
            xmlWriter.writeElementString("OrgReqTime", Format.DateTime14Format.format(Format.DateTimeFormat.parse(p_req1944.bgnDate)));

            xmlWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlWriter.toXmlString(), false);
//            String[] bankResult = new String[]{"", ""};
//            bankResult[1] = FileHelper.readFileText("C:\\Users\\F\\Desktop\\tmp.xml", "gbk");
//            WriteBankLogStep3(bankResult[1]);
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String rsp_no = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String rsp_msg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(rsp_no)) {
                            detail.transState = rsp_no+"+"+tmpBkReader.getFirstText("/Message/Body/List/Map/Status");
                        }else {
                            detail.transState = rsp_no;
                        }
                        detail.respCode = "0";
                        detail.respInfo = rsp_msg;
                        detail.reqSeqID = p_req1944.askNumber;
                        detail.payInfoCode = detail.transState;
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
